import * as THREE from "three"
import { OBJECTS } from "../../app/declarations.js"

import { EffectComposer } from "../../vendor/threejs/postprocessing/EffectComposer.js"
import { SSRPass } from "../../vendor/threejs/postprocessing/SSRPass.js"
import { SSAOPass } from "../../vendor/threejs/postprocessing/SSAOPass.js"
import { UnrealBloomPass } from "../../vendor/threejs/postprocessing/UnrealBloomPass.js"
import { RenderPass } from "../../vendor/threejs/postprocessing/RenderPass"
import { ShaderPass } from "../../vendor/threejs/postprocessing/ShaderPass.js"
import { GammaCorrectionShader } from "../../vendor/threejs/shaders/GammaCorrectionShader.js"
import { ReflectorForSSRPass } from "../../vendor/threejs/objects/ReflectorForSSRPass.js"

import { UTILS } from "../../app/declarations.js"
import { CONFIG } from "../config.js"

let POSTPROCESSING = {
  init: () => {
    const scene = OBJECTS.scene
    const camera = OBJECTS.camera
    const renderer = OBJECTS.renderer

    OBJECTS.postprocessing.composer = new EffectComposer(OBJECTS.renderer)
    // OBJECTS.postprocessing.composer.setSize(UTILS.width, UTILS.height)

    if (CONFIG.postprocessing.enabled == true) {
      for (var key in CONFIG.postprocessing.effects) {
        let effect = CONFIG.postprocessing.effects[key]

        switch (effect) {
          case "SSR":
            pp.ssrpass = new SSRPass({
              renderer,
              scene,
              camera,
              width: UTILS.sw,
              height: UTILS.sh,
            })

            OBJECTS.postprocessing.composer.addPass(pp.ssrpass)
            OBJECTS.postprocessing.composer.addPass(
              new ShaderPass(GammaCorrectionShader)
            )
            break

          case "SSAO":
            pp.ssaopass = new SSAOPass(
              OBJECTS.scenes.mainScene,
              OBJECTS.camera,
              UTILS.width / 2,
              UTILS.height / 2
            )
            pp.ssaopass.kernelRadius = 16
            OBJECTS.postprocessing.composer.addPass(pp.ssaopass)

            break

          case "Bloom":
            const params = {
              exposure: 1,
              bloomStrength: 1.5,
              bloomThreshold: 0,
              bloomRadius: 0,
            }
            const bloomPass = new UnrealBloomPass(
              new THREE.Vector2(window.innerWidth, window.innerHeight),
              1.5,
              0.4,
              0.85
            )

            const renderScene = new RenderPass(
              OBJECTS.scenes.mainScene,
              OBJECTS.camera
            )

            bloomPass.threshold = params.bloomThreshold
            bloomPass.strength = params.bloomStrength
            bloomPass.radius = params.bloomRadius

            OBJECTS.postprocessing.composer.addPass(bloomPass)
            OBJECTS.postprocessing.composer.addPass(renderScene)

            break
        }
      }
    }

    // console.log(OBJECTS.postprocessing.composer)
  },
}

export { POSTPROCESSING }
